package com.hp.bon.sgw.stdbss30.bizSlot;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.bean.BillQueryOweResponse;
import com.hp.bon.sgw.stdbss30.common.BizConsts;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import com.hp.bon.sgw.stdbss30.utils.HttpClientUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @PROJECT_NAME: SGW_2.1
 * @DESCRIPTION:
 * @USER: YouQi
 * @DATE: 2021/5/19 10:32
 */
public class BillQueryOweBizSlot implements BizSlot {
    private static final Logger LOG = LoggerFactory.getLogger(BillQueryOweBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        Map<String, Object> msgBody = new HashMap<>();

        String billQueryType = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Destination_Id_Type");
        msgBody.put("billQueryType", billQueryType);
        String destinationAccount = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Destination_Id");
        msgBody.put("destinationAccount", destinationAccount);
        String destinationAttr = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/DestinationAttr");
        msgBody.put("destinationAttr", destinationAttr);
        String feeQueryFlag = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Fee_Query_Flag");
        msgBody.put("feeQueryFlag", feeQueryFlag.isEmpty()?0:feeQueryFlag);
        String queryFlag = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Query_Flag");
        msgBody.put("queryFlag", queryFlag);
        String balanceQueryFlag = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/BalanceQueryFlag");
        msgBody.put("balanceQueryFlag", balanceQueryFlag);

        Map<String, String> operAttrStruct = new HashMap<>();
        operAttrStruct.put("operServiceId", "1");
        operAttrStruct.put("lanId", "8420100");
        operAttrStruct.put("operPost", "0");
        operAttrStruct.put("operOrgId", "0");
        operAttrStruct.put("staffId", "0");
        operAttrStruct.put("operTime", "0");
        msgBody.put("operAttrStruct", operAttrStruct);



        String msgBodyStr = JSON.toJSONString(msgBody);

        String url = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);

      		LOG.info("request body is {}", msgBodyStr.toString());
            LOG.info("webserviceUrl is {}", url);
            String respStr = HttpClientUtils.postHttpStr(msgBodyStr.toString(), url);
            LOG.info("response is {}", respStr);

        if ("".equals(respStr)) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            return bizResult;
        }

        BillQueryOweResponse billQueryOweResponse = new Gson().fromJson(respStr, BillQueryOweResponse.class);
        TreeMap<Object, Object> respDataXml = new TreeMap<>();
        respDataXml.put("Result", billQueryOweResponse.getResultCode());
        respDataXml.put("Acct_ID", billQueryOweResponse.getAcctId());
        respDataXml.put("Acct_Name",billQueryOweResponse.getAcctName());
        respDataXml.put("Should_Charge",billQueryOweResponse.getShouldCharge());
        List<BillQueryOweResponse.BillQueryInformation> billQueryInformation = billQueryOweResponse.getBillQueryInformation();
        String bodyItem = "";
        if (billQueryInformation != null) {
            for (BillQueryOweResponse.BillQueryInformation query : billQueryInformation) {
                SortedMap<Object, Object> billQueryInfoXml = new TreeMap<>();
                billQueryInfoXml.put("Owe_Id", query.getOweId());
                billQueryInfoXml.put("Destination_Id", query.getDestinationId());
                billQueryInfoXml.put("Fee_Cycle_Id", query.getFeeCycleId());
                billQueryInfoXml.put("State", query.getState());
                billQueryInfoXml.put("StateDate", query.getStateDate());
                billQueryInfoXml.put("Total_Charge", query.getTotalCharge());
                billQueryInfoXml.put("Charge_Payed", query.getChargePayed());
                billQueryInfoXml.put("Billing_Cycle", query.getBillingCycle());
                billQueryInfoXml.put("Acct_Item_Type_Id", query.getAcctItemTypeId());
                billQueryInfoXml.put("Acct_Item_Type_Name", query.getAcctItemTypeName());
                billQueryInfoXml.put("State", query.getState());
                try {
                    bodyItem+="<OWE_ITEM>"+HttpClientUtils.mapToSignXml(billQueryInfoXml)+"</OWE_ITEM>";
                } catch (Exception e) {
                    bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
                    LOG.error("Stitching XML error", e);
                    return bizResult;
                }
            }
        }
        String respXmlStr="";
        try {
            String respDataXmlStr = HttpClientUtils.mapToSignXml(respDataXml);
            respDataXmlStr += bodyItem;
            respXmlStr = HttpClientUtils.directAck(xmlMessage, respDataXmlStr);

        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            LOG.error("Stitching XML error", e);
            return bizResult;
        }
        bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
        bizResult.setHttpBody(respXmlStr);
        return bizResult;
    }
}
